package cn.chrilwe.pigpig.user.service;


import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.chrilwe.pigpig.core.model.user.Authority;
import cn.chrilwe.pigpig.core.model.user.Role;
import cn.chrilwe.pigpig.core.model.user.RoleAuth;
import cn.chrilwe.pigpig.core.response.BaseResult;
import cn.chrilwe.pigpig.user.common.ResponseEnum;
import cn.chrilwe.pigpig.user.common.exception.AuthorityNullPointException;
import cn.chrilwe.pigpig.user.common.exception.AuthorityParamErr;
import cn.chrilwe.pigpig.user.mapper.AuthorityMapper;
import cn.chrilwe.pigpig.user.mapper.RoleMapper;
import cn.chrilwe.pigpig.user.request.JoinRolePermissionRequest;

@Service
@Transactional
public class PermissionService {
	
	@Autowired
	private AuthorityMapper authorityMapper;
	
	@Autowired
	private RoleMapper roleMapper;

	//角色关联权限
	public BaseResult roleJoinPermission(JoinRolePermissionRequest request) {
		if(request != null) {
			int authId = request.getAuthId();
			int roleId = request.getRoleId();
			if(authId == 0 || roleId == 0) {
				return new BaseResult(ResponseEnum.PARAM_ERR.getCode(),ResponseEnum.PARAM_ERR.getName());
			}
			Role role = roleMapper.selectById(roleId);
			if(role == null) {
				return new BaseResult(ResponseEnum.ROLE_NOT_EXISTS.getCode(),ResponseEnum.ROLE_NOT_EXISTS.getName());
			}
			Authority authority = authorityMapper.selectById(authId);
			if(authority == null) {
				return new BaseResult(ResponseEnum.AUTHORITY_NO_EXISTS.getCode(),ResponseEnum.AUTHORITY_NO_EXISTS.getName());
			}
			RoleAuth roleAuth = roleMapper.selectRoleAuthByRoleId(roleId);
			roleAuth = new RoleAuth();
			roleAuth.setAuthId(authId);
			roleAuth.setRoleId(roleId);
			if(roleAuth != null) {
				roleMapper.updateRoleAuth(roleAuth);
			} else {
				roleMapper.insertRoleAuth(roleAuth);
			}
			return new BaseResult(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getName());
		}
		return null;
	}

	//插入权限信息
	public BaseResult addPermission(Authority authority) {
		BaseResult res = null;
		try {
			checkAuthority(authority);
		} catch (Exception e) {
			if(e instanceof AuthorityNullPointException || e instanceof AuthorityParamErr) {
				res = new BaseResult(ResponseEnum.PARAM_ERR.getCode(),ResponseEnum.PARAM_ERR.getName());
			} else {
				res = new BaseResult(ResponseEnum.UNKNOW_ERR.getCode(),ResponseEnum.UNKNOW_ERR.getName());
			}
			return res;
		}
		authorityMapper.insertAuthority(authority);
		return res = new BaseResult(ResponseEnum.SUCCESS.getCode(),ResponseEnum.SUCCESS.getName());
	}
	
	private void checkAuthority(Authority authority) {
		if(authority != null) {
			String name = authority.getName();
			String url = authority.getUrl();
			if(StringUtils.isAnyEmpty(name,url)) {
				throw new AuthorityParamErr();
			}
			authorityMapper.insertAuthority(authority);
		} else {
			throw new AuthorityNullPointException("null");
		}
	}

	//更新权限
	public void updatePermission() {
		
	}
	
	//删除权限
	public void deletePermissionById(int authId) {
		authorityMapper.deleteAuthorityById(authId);
		roleMapper.deleteRoleAuthByAuthId(authId);
	}
}
